﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.ObjectModel;

namespace VRacer
{
    /// <summary>
    /// Zawiera wszystkie trasy wczytane przez gre. Moze je zapisywac i odczytywac z plikow XML za pomoca statycznych metod (Create i Save).
    /// </summary>
    public class TracksCollection : ObservableCollection<Track>
    {
        private Dictionary<string, Track> _tracks = new Dictionary<string, Track>();



        public Track CreateTrack()
        {
            return new Track();
        }

        /// <summary>
        /// Inserts an item into the collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
        /// <param name="item">The object to insert.</param>
        protected override void InsertItem(int index, Track item)
        {
            base.InsertItem(index, item);
            _tracks.Add(item.ID, item);
        }

        public Track this[string id]
        {
            get { return _tracks.ContainsKey(id) ? _tracks[id] : null; }
        }

        public static readonly TracksCollection Tracks = TracksCollection.Create("Tracks/");
        //public static TracksCollection Tracks { get { return _tracks; } }

        /// <summary>
        /// Wczytuje wszystkie trasy z folderu
        /// </summary>
        /// <param name="path">Pelna sciezka do folderu z mapami</param>
        /// <returns>Kolekcja z wczytanymi trasami</returns>
        /// <exception cref="Exceptions.TracksNotFoundException" />
        public static TracksCollection Create(string path)
        {
            try
            {
                //pobierz wszystkie katalogi (kazdy katalog to potencjalna trasa)
                string[] dirs = Directory.GetDirectories(path);

                var tracks = new TracksCollection();

                foreach (string dir in dirs)
                {
                    //jesli zawiera plik definicji trasy
                    if (File.Exists(dir + "/track.xml"))
                    {
                        //przeparsuj definicje trasy
                        try
                        {
                            Track track = Track.Create(dir + "/track.xml");
                            track.ContainingDirectory = dir + "/";
                            tracks.Add(track);
                            AIDraw.Add(track);
                        }
                        catch (Exception e)
                        {
                            throw new Exceptions.CannotReadTrackException(e, dir);
                        }
                    }
                }

                return tracks;
            }
            catch (Exceptions.CannotReadTrackException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new Exceptions.CannotReadTracksException(e);
            }
        }
    }
}
